package com.edgar.loglib;

import android.app.ActivityManager;
import android.content.Context;

import java.util.Date;
import java.util.List;

/**
 * Created by edgar on 2016/1/17.
 */
public class Utils {
    /**
     * 根据date生成日志文件名
     * @param date
     * @return
     */
    static String generateFileName(Date date){
        StringBuilder fileNameBuild = new StringBuilder();
        fileNameBuild.append(Constants.LOG_FILE_PREFIX)
                .append(Constants.LOG_FILE_FORMAT.format(date))
                .append(Constants.LOG_FILE_SUFFIX);
        return fileNameBuild.toString();
    }

    public static String getCurrentProcessName(Context context){
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
        if(runningAppProcesses != null && runningAppProcesses.size() > 0){
            int processId = android.os.Process.myPid();
            for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses){
                if(runningAppProcessInfo != null && runningAppProcessInfo.pid == processId){
                    return runningAppProcessInfo.processName;
                }
            }
        }
        return "";
    }
}